/**
 * JZ UnoCSS - Transform处理器
 * 处理Transform相关的类名组合和CSS生成
 * @version 1.0.0
 * @author jz
 */

import { generateCSS } from '../core/generator.js';
import { utils } from '../utils/utils.js';

/**
 * 处理 transform 组合类名
 * @param {string[]} classNames - 类名数组
 * @param {Object} options - 选项
 * @returns {Object|null} Transform信息对象
 */
export function processTransformClasses(classNames, options = {}) {
    // 查找 transform 相关的类名
    let hasTransformBase = false;
    const transformFunctions = {};
    const transformClasses = [];
    let transformOrigin = null;
    
    for (const className of classNames) {
        const decodedClassName = utils.decodeClassNameFromUvue(className);
        
        // 检查是否是 transform 相关类名
        if (decodedClassName === 'transform') {
            hasTransformBase = true;
            transformClasses.push(className);
        } else if (decodedClassName.match(/^(rotate|scale|translate|skew)-/)) {
            // 生成transform函数
            const styles = generateCSS(decodedClassName, options);
            if (styles) {
                // 查找 transform 相关的特殊属性
                for (const [key, value] of Object.entries(styles)) {
                    if (key.startsWith('__transform-')) {
                        transformFunctions[key] = value;
                    }
                }
            }
            transformClasses.push(className);
        } else if (decodedClassName.match(/^origin-/)) {
            // 处理 transform-origin
            const styles = generateCSS(decodedClassName, options);
            if (styles && styles['transform-origin']) {
                transformOrigin = styles['transform-origin'];
            }
            transformClasses.push(className);
        }
    }
    
    // 如果没有 transform 基础类或变换函数，不处理
    if (!hasTransformBase || Object.keys(transformFunctions).length === 0) {
        return null;
    }
    
    // 按顺序组装 transform 函数
    const transformOrder = [
        '__transform-translate-x', '__transform-translate-y', 
        '__transform-rotate', '__transform-rotate-x', '__transform-rotate-y', '__transform-rotate-z', 
        '__transform-scale', '__transform-scale-x', '__transform-scale-y', 
        '__transform-skew-x', '__transform-skew-y'
    ];
    const transformValues = [];
    
    for (const key of transformOrder) {
        if (transformFunctions[key]) {
            transformValues.push(transformFunctions[key]);
        }
    }
    
    if (transformValues.length === 0) {
        return null;
    }
    
    // 生成组合选择器
    const selector = transformClasses.map(cls => `.${cls}`).join('');
    
    // 根据模式决定CSS格式
    const isProduction = options.mode === 'production';
    
    if (isProduction) {
        // 生产模式：压缩格式
        let cssProperties = `transform:${transformValues.join(' ')}`;
        if (transformOrigin) {
            cssProperties += `;transform-origin:${transformOrigin}`;
        }
        const styleRule = `${selector}{${cssProperties}}`;
        
        return {
            selector: transformClasses.join('.'),
            styleRule: styleRule,
            classNames: transformClasses
        };
    } else {
        // 开发模式：格式化
        let cssProperties = `  transform: ${transformValues.join(' ')};`;
        if (transformOrigin) {
            cssProperties += `\n  transform-origin: ${transformOrigin};`;
        }
        const styleRule = `${selector} {\n${cssProperties}\n}`;
        
        return {
            selector: transformClasses.join('.'),
            styleRule: styleRule,
            classNames: transformClasses
        };
    }
}

/**
 * 生成条件编译Transform CSS
 * @param {Object} transformInfo - Transform信息
 * @param {Object} options - 选项
 * @returns {string} 带条件编译的Transform CSS
 */
export function generateConditionalTransformCSS(transformInfo, options = {}) {
    const { mode = 'development' } = options;
    const isProduction = mode === 'production';
    const selector = transformInfo.selector;
    
    // 提取transform相关属性
    const transformMatch = transformInfo.styleRule.match(/transform:\s*([^;}]+)/);
    const originMatch = transformInfo.styleRule.match(/transform-origin:\s*([^;}]+)/);
    
    if (!transformMatch) return '';
    
    const transformValue = transformMatch[1];
    const originValue = originMatch ? originMatch[1] : null;
    
    // Transform在所有平台都是通用的，不需要条件编译
    if (isProduction) {
        let cssProps = `transform:${transformValue}`;
        if (originValue) cssProps += `;transform-origin:${originValue}`;
        return `.${selector}{${cssProps}}`;
    } else {
        let css = `.${selector} {\n`;
        css += `  transform: ${transformValue};\n`;
        if (originValue) css += `  transform-origin: ${originValue};\n`;
        css += `}`;
        return css;
    }
}

/**
 * 检查类名组合是否包含Transform
 * @param {string[]} group - 类名组合
 * @returns {boolean} 是否包含Transform
 */
export function hasTransformClasses(group) {
    const hasTransformBase = group.some(cls => {
        const decoded = utils.decodeClassNameFromUvue(cls);
        return decoded === 'transform';
    });
    
    const hasTransformFunction = group.some(cls => {
        const decoded = utils.decodeClassNameFromUvue(cls);
        return decoded.match(/^(rotate|scale|translate|skew|origin)-/);
    });
    
    return hasTransformBase && hasTransformFunction;
} 